import Vue from 'vue';
Date.prototype.format = function(fmt) {
	const o = {
		"M+": this.getMonth() + 1, //月份
		"d+": this.getDate(), //日
		"h+": this.getHours(), //小时
		"m+": this.getMinutes(), //分
		"s+": this.getSeconds(), //秒
		"q+": Math.floor((this.getMonth() + 3) / 3), //季度
		"S": this.getMilliseconds() //毫秒
	};
	if (/(y+)/.test(fmt))
		fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	for (const k in o)
		if (new RegExp("(" + k + ")").test(fmt))
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
	return fmt;
};
Date.prototype.lastDate = function() {
	const fullYear = this.getFullYear();
	const month = this.getMonth() + 1;
	return new Date(fullYear, month, 0);
};
Date.prototype.calendar = function() {
	let date = new Date(this.setDate(1))
	let startDay = date.getDay();
	let startDate = new Date(this.getTime() - 24 * 60 * 60 * 1000 * startDay + 24 * 60 * 60 * 1000)
	date = this.lastDate()
	let endDay = date.getDay() ? date.getDay() : 7;
	let endDate = new Date(date.getTime() + 24 * 60 * 60 * 1000 * (7 - endDay))
	let calendar = []
	date = startDate
	while (endDate.getTime() > date.getTime()) {
		calendar.push(date)
		date = new Date(date.getTime() + 24 * 60 * 60 * 1000)
	}
	calendar.push(endDate)
	return calendar
}
Vue.prototype.$getUpdateTime = function(updateTime) {
	if (updateTime === null) {
		return '';
	}
	let now = new Date().getTime();
	let second = Math.floor(now / 1000 - updateTime);
	let minute = Math.floor(second / 60);
	let hour = Math.floor(minute / 60);
	let day = Math.floor(hour / 24);
	let month = Math.floor(day / 31);
	let year = Math.floor(month / 12);
	if (year > 0) {
		return year + '年前';
	} else if (month > 0) {
		return month + '月前';
	} else if (day > 0) {
		let ret = day + '天前';
		if (day >= 7 && day < 14) {
			ret = '1周前';
		} else if (day >= 14 && day < 21) {
			ret = '2周前';
		} else if (day >= 21 && day < 28) {
			ret = '3周前';
		} else if (day >= 28 && day < 31) {
			ret = '4周前';
		}
		return ret;
	} 
	// else {
	// 	return new Date(updateTime * 1000).format('hh:mm');
	// }
	else if (hour > 0) {
		return hour + '小时前';
	} else if (minute > 0) {
		return minute + '分钟前';
	} else if (second > 0) {
		return second + '秒前';
	} else {
		return '刚刚';
	}
}
export default {
	init() {
		console.log("*** date installed ***")
	}
}
